<?php
class ControllerFactory implements Factory{

	const SEPERATOR = '_';
	const _SUFFIX = "Controller";
	/**
	 * Controller.
	 * @var String
	 */
	private $_context;

	/**
	 * Controller
	 * @var BaseController
	 */
	private $_controller;

	/**
	 * (non-PHPdoc)
	 * @see library/Factory/Factory::getInstance()
	 * @param ControllerRoute $route
	 * @return Controller
	 */
	public function getInstance($route){
		return null;
	}
	/**
	 * Enter description here ...
	 * @param string $context
	 * @return BaseController
	 */
	public static function instance($context){
		$cname = $context[Configuration::CONTROLLER] . ControllerFactory::_SUFFIX;
		$controllerPath = Configuration::APPLICATION_FOLDER
		. SLASH
		. $context[Configuration::MODULE]
		. SLASH
		. Configuration::CONTROLLER_FOLDER
		. SLASH
		. $cname .'.php';
		$class = $context[Configuration::MODULE] . ControllerFactory::SEPERATOR . $cname;
		if(file_exists($controllerPath)){
			include_once $controllerPath;
			if(!class_exists($class)){
				Log::error("ControllerFactory", "Class " . $class . " Not found");
//				if(Configuration::DEBUG_MOD)
//				throw new ControllerException("Class " . $class . " Not found");
				$cname = Configuration::CONTROLLER_DEFAULT . ControllerFactory::_SUFFIX;
				$class = $context[Configuration::MODULE] . ControllerFactory::SEPERATOR . $cname;
			}
		}
		else{
			Log::error("ControllerFactory", "File " . $controllerPath . " Not found");
			$cname = Configuration::CONTROLLER_DEFAULT . ControllerFactory::_SUFFIX;
			$class = $context[Configuration::MODULE] . ControllerFactory::SEPERATOR . $cname;
			$controllerPath = Configuration::APPLICATION_FOLDER
			. SLASH
			. $context[Configuration::MODULE]
			. SLASH
			. Configuration::CONTROLLER_FOLDER
			. SLASH
			. Configuration::CONTROLLER_DEFAULT
			. ControllerFactory::_SUFFIX
			.'.php';
			include_once $controllerPath;
		}
		return new $class;
	}
}